//adw::ToolbarView is a replacement for adw::Dialog but not suitable for an async flow

// Imports
use crate::{config, RnAppWindow};
use adw::prelude::*;
use futures::StreamExt;
use gettextrs::gettext;
use gtk4::{
    gio, glib, glib::clone, Builder, Button, CallbackAction, FileDialog, FileFilter, Label,
    Shortcut, ShortcutController, ShortcutTrigger, ToggleButton,
};
use num_traits::ToPrimitive;
use tracing::{debug, error};

/// Opens a new mytool save file in a new tab
pub(crate) async fn filedialog_open_doc(appwindow: &RnAppWindow) {
    let filter = FileFilter::new();
    // note : mimetypes are not supported with the native file picker on windows
    // See the limitations on FileChooserNative
    // https://gtk-rs.org/gtk3-rs/stable/latest/docs/gtk/struct.FileChooserNative.html#win32-details--gtkfilechooserdialognative-win32
    if cfg!(target_os = "windows") {
        filter.add_pattern("*.mytool");
    } else {
        filter.add_mime_type("application/mytool");
    }
    filter.add_suffix("mytool");
    filter.set_name(Some(&gettext(".mytool")));

    let filter_list = gio::ListStore::new::<FileFilter>();
    filter_list.append(&filter);

    let filedialog = FileDialog::builder()
        .title(gettext("Open File"))
        .modal(true)
        .accept_label(gettext("Open"))
        .filters(&filter_list)
        .default_filter(&filter)
        .build();
    //
    // if let Some(current_workspace_dir) = appwindow.sidebar().workspacebrowser().dir_list_dir() {
    //     filedialog.set_initial_folder(Some(&gio::File::for_path(current_workspace_dir)));
    // }

    match filedialog.open_future(Some(appwindow)).await {
        Ok(selected_file) => {
            //     appwindow
            //         .open_file_w_dialogs(selected_file, None, true)
            //         .await;
        }
        Err(e) => {
            debug!("Did not open document (Error or dialog dismissed by user), Err: {e:?}");
        }
    }
}

pub(crate) async fn filedialog_import_file(appwindow: &RnAppWindow) {
    let filter = FileFilter::new();
    // note : mimetypes are not supported with the native file picker on windows
    // See the limitations on FileChooserNative
    // https://gtk-rs.org/gtk3-rs/stable/latest/docs/gtk/struct.FileChooserNative.html#win32-details--gtkfilechooserdialognative-win32
    if cfg!(target_os = "windows") {
        filter.add_pattern("*.xopp");
        filter.add_pattern("*.pdf");
        filter.add_pattern("*.svg");
        filter.add_pattern("*.png");
        filter.add_pattern("*.jpeg");
        filter.add_pattern("*.txt");
    } else {
        filter.add_mime_type("application/x-xopp");
        filter.add_mime_type("application/pdf");
        filter.add_mime_type("image/svg+xml");
        filter.add_mime_type("image/png");
        filter.add_mime_type("image/jpeg");
        filter.add_mime_type("text/plain");
    }
    filter.add_suffix("xopp");
    filter.add_suffix("pdf");
    filter.add_suffix("svg");
    filter.add_suffix("png");
    filter.add_suffix("jpg");
    filter.add_suffix("jpeg");
    filter.add_suffix("txt");
    filter.set_name(Some(&gettext("Jpg, Pdf, Png, Svg, Xopp, Txt")));

    let filter_list = gio::ListStore::new::<FileFilter>();
    filter_list.append(&filter);

    let dialog = FileDialog::builder()
        .title(gettext("Import File"))
        .modal(true)
        .accept_label(gettext("Import"))
        .filters(&filter_list)
        .default_filter(&filter)
        .build();

    // if let Some(current_workspace_dir) = appwindow.sidebar().workspacebrowser().dir_list_dir() {
    //     dialog.set_initial_folder(Some(&gio::File::for_path(current_workspace_dir)));
    // }

    match dialog.open_future(Some(appwindow)).await {
        Ok(selected_file) => {
            // appwindow
            //     .open_file_w_dialogs(selected_file, None, true)
            //     .await;
        }
        Err(e) => {
            debug!("Did not import file (Error or dialog dismissed by user), Err: {e:?}");
        }
    }
}
